package com.example.superoa.model.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.dao.SalaryAccountDao;
import com.example.superoa.model.dao.SalaryDetailDao;
import com.example.superoa.model.dao.SalaryPayDao;
import com.example.superoa.model.dao.SalarySubDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.mappers.PayMapper;
import com.example.superoa.model.pojos.SalaryAccout;
import com.example.superoa.model.pojos.SalaryDetail;
import com.example.superoa.model.pojos.SalaryPay;
import com.example.superoa.model.pojos.SalarySub;
import com.example.superoa.model.pojos.Staff;

@Service
@Transactional(rollbackFor=Exception.class)
public class PayService {
	@Autowired
	private PayMapper pm;
	@Autowired
	private SalaryPayDao spd;
	@Autowired
	private SalarySubDao ssd;
	@Autowired
	private SalaryAccountDao sad;
	@Autowired
	private StaffDao sd;
	@Autowired
	private SalaryDetailDao sdd;
	@Autowired
	private StaffDao std;
	
	public JSONArray findpay() {
		return pm.findpay();
	}
	public JSONArray findthems(int account_id) {
		return pm.findthems(account_id);
	}
	//添加
	public void tianjia(SalaryPay sp) {
		SalaryPay pay = new SalaryPay();
		//发放月份
		pay.setSpMonth(new Date());
		//发放主题
		pay.setSpThems(sp.getSpThems());		
		//送审记录表
		SalarySub sub = new SalarySub();
		sub.setSubStatic(3);
		//套账表
		SalaryAccout account = new SalaryAccout();
		pay.setSalaryAccout(account);
		//员工表
		Staff s=new Staff();
		pay.setStaff(s);		
		
		spd.save(pay);
	}
	//发放查看
	public JSONArray pay_chakan(int account_id) {
		return pm.pay_chakan(account_id);
	}
	//获取套账所属员工
	public JSONArray pay_accuntname(int account_id) {
		return pm.pay_accountname(account_id);
	}
	//状态为已创建的删除
	public void shanchu(int pay_id) {
		SalaryPay pay=spd.findById(pay_id).get();	
		for (SalarySub entity : pay.getSalarySubs()) {
			ssd.deleteById(entity.getSubId());
		}
		spd.deleteById(pay_id);
	}
	//选择套账
	public JSONArray selectcounts() {
		return pm.selectcounts();
	}
	//新增发放
	public void xinzengpay(SalaryPay spay,int sa_id,int staff_id) {
		//套账
		SalaryAccout sas=sad.findById(sa_id).get();
		//员工
		Staff s=std.findById(staff_id).get();
		//发放
		SalaryPay pay=new SalaryPay();
		pay.setSpThems(spay.getSpThems());
		pay.setSpMonth(new Date());
		pay.setRealHair(new BigDecimal(0));
		pay.setSpPay(new BigDecimal(0));
		pay.setSpDucted(new BigDecimal(0));
		spd.save(pay);
	}  
	public void kq_month(int account_id,int sub_id,int pay_id) {
		int i=pm.sd_id()+1;
		List<Integer> accounts = pm.paymingxi(account_id);	
		for (Integer object : accounts) {
			jisua(object,pay_id,account_id,i);
			i++;
		}	
		//点击发放状态修改为3
		SalarySub ss=ssd.findById(sub_id).get();
		ss.setSubStatic(3);	
		ssd.save(ss);
	}
	public void jisua(int staff_id,int pay_id,int account_id,int id) {
		JSONObject sadate = pm.findaccountdate(pay_id);
		Date sdate = sadate.getDate("sp_month");
		int month3=sdate.getMonth();
		int year3=sdate.getYear()+1900;
		JSONObject kq = pm.findstaff(staff_id,month3,year3);
		if(kq==null) {
			//新增到发放详情表
			SalaryDetail sd=new SalaryDetail();
			sd.setSdId(id);
			//迟到扣
			sd.setSdLate(new BigDecimal(0));
			//早退扣
			sd.setSdLe(new BigDecimal(0));
			//旷工扣
			sd.setSdAbsent(new BigDecimal(0));
			//缺勤扣
			sd.setSdTraffic(new BigDecimal(0));
			//加班加
			sd.setSdOvertime(new BigDecimal(0));		
			//请假扣
			sd.setSdVacation(new BigDecimal(0));			
			//全勤奖
			sd.setSdBonus(new BigDecimal(0));		
			//合计
			sd.setSdCount(new BigDecimal(0));
			//实发
			sd.setSdSf(new BigDecimal(0));
			//应发			
			sd.setSdYf(new BigDecimal(0));
			//实扣
			sd.setSdYk(new BigDecimal(0));		
			Staff sta=std.findById(staff_id).get();
			SalaryPay sp=spd.findById(pay_id).get();
			sd.setSalaryPay(sp);
			sd.setStaff(sta);		
			sdd.save(sd);
		}else if(kq.getBigDecimal("AM_KUANG").compareTo(new BigDecimal(7))>=0){
			//新增到发放详情表
			SalaryDetail sd=new SalaryDetail();
			sd.setSdId(id);
			//迟到扣
			sd.setSdLate(new BigDecimal(0));
			//早退扣
			sd.setSdLe(new BigDecimal(0));
			//旷工扣
			sd.setSdAbsent(new BigDecimal(0));
			//缺勤扣
			sd.setSdTraffic(new BigDecimal(0));
			//加班加
			sd.setSdOvertime(new BigDecimal(0));		
			//请假扣
			sd.setSdVacation(new BigDecimal(0));			
			//全勤奖
			sd.setSdBonus(new BigDecimal(0));		
			//合计
			sd.setSdCount(new BigDecimal(0));
			//实发
			sd.setSdSf(new BigDecimal(0));
			//应发			
			sd.setSdYf(new BigDecimal(0));
			//实扣
			sd.setSdYk(new BigDecimal(0));		
			Staff sta=std.findById(staff_id).get();
			SalaryPay sp=spd.findById(pay_id).get();
			sd.setSalaryPay(sp);
			sd.setStaff(sta);		
			sdd.save(sd);
		}else {
			//staff_id=1的员工基本工资
			JSONObject account = pm.findbase(staff_id);
			BigDecimal base=account.getBigDecimal("sa_base");		
			BigDecimal yidao=kq.getBigDecimal("AM_YINGCHU");	
			BigDecimal day = base.divide(yidao,2,RoundingMode.HALF_UP);
			BigDecimal hour = day.divide(new BigDecimal(8),2,RoundingMode.HALF_UP);
			//迟到   迟到【1次*50￥】扣
			BigDecimal chi_ci=kq.getBigDecimal("AM_CHI");
			BigDecimal chi_money=chi_ci.multiply(new BigDecimal(50));
			//早退  	早退【早退次数*50】扣
			BigDecimal zao_ci = kq.getBigDecimal("AM_TUI");
			BigDecimal zao_money = zao_ci.multiply(new BigDecimal(50));
			//旷工【扣除一天基本工资*1.5】扣
			BigDecimal kuang_ci=kq.getBigDecimal("AM_KUANG");
			BigDecimal kuang_money=kuang_ci.multiply(new BigDecimal(1.5)).multiply(day);
			//缺勤【缺勤一天】
			BigDecimal queqing=kq.getBigDecimal("AM_QUE");
			BigDecimal qq_money=queqing.multiply(new BigDecimal(1.5)).multiply(day);
			//休息日加班【一小时的工资*加班小时*1.5】
			BigDecimal xiu_shi = kq.getBigDecimal("AM_XIUJIA");
			BigDecimal xiu_money = hour.multiply(xiu_shi).multiply(new BigDecimal(1.5));		
			//平时加班【一小时的工资*加班小时】
			BigDecimal ping_shi = kq.getBigDecimal("AM_GONGJIA");
			BigDecimal ping_money = ping_shi.multiply(hour);
			//节假日加班【一小时的工资*加班小时*2】
			BigDecimal jie_shi = kq.getBigDecimal("AM_JIEJIA");
			BigDecimal jie_money=jie_shi.multiply(hour).multiply(new BigDecimal(2));
			//所有加班费
			BigDecimal syjb = xiu_money.add(ping_money).add(jie_money);
			
			//请假【请假一天*一天的基本工资】
			BigDecimal qing_ci = kq.getBigDecimal("AM_QING");
			BigDecimal qing_money = day.multiply(qing_ci);
			//全勤奖
			BigDecimal shi_ci = kq.getBigDecimal("AM_SHICHU");
			BigDecimal shi_moeny;
			if(shi_ci.compareTo(yidao)>=0) {
				shi_moeny=new BigDecimal(200);
			}else {
				shi_moeny=new BigDecimal(0);
			}
			
			//新增到发放详情表
			SalaryDetail sd=new SalaryDetail();
			sd.setSdId(id);
			//迟到扣
			sd.setSdLate(new BigDecimal(0).subtract(chi_money));
			//早退扣
			sd.setSdLe(new BigDecimal(0).subtract(zao_money));
			//旷工扣
			sd.setSdAbsent(new BigDecimal(0).subtract(kuang_money));
			//缺勤扣
			sd.setSdTraffic(new BigDecimal(0).subtract(qq_money));
			//加班加
			sd.setSdOvertime(syjb);		
			//请假扣
			sd.setSdVacation(new BigDecimal(0).subtract(qing_money));			
			//全勤奖
			sd.setSdBonus(shi_moeny);		
			
			//获取薪资项目下的套账
			JSONObject itembase=pm.jisuanitem(account_id);
			BigDecimal bases = itembase.getBigDecimal("si_money");
			//合计		
			BigDecimal heji = bases.add(base).add(shi_moeny).add(syjb).subtract(zao_money).subtract(qing_money).subtract(chi_money).subtract(kuang_money).subtract(qq_money);
			sd.setSdCount(heji);
			//实发
			sd.setSdSf(heji);
			//应发
			BigDecimal yf=base.add(shi_moeny).add(syjb);
			sd.setSdYf(yf);
			//实扣
			BigDecimal sk = new BigDecimal(0).subtract(zao_money).subtract(qing_money).subtract(chi_money).subtract(kuang_money).subtract(qq_money).subtract(bases);
			sd.setSdYk(sk);	
			Staff sta=std.findById(staff_id).get();
			SalaryPay sp=spd.findById(pay_id).get();
			sd.setSalaryPay(sp);
			sd.setStaff(sta);		
			sdd.save(sd);	
		}
	}
	public JSONArray payde(int pay_id) {
		return pm.payde(pay_id);
	}
	public void xzpay(String sp_thems,int account_id,int staff_id) {			
			SalaryPay sp=new SalaryPay();
			sp.setSpMonth(new Date());
			sp.setSpThems(sp_thems);
			sp.setSpPay(new BigDecimal(0));
			sp.setSpDucted(new BigDecimal(0));
			sp.setRealHair(new BigDecimal(0));
			SalaryAccout sa = sad.findById(account_id).get();
			sp.setSalaryAccout(sa);
			Staff s=sd.findById(staff_id).get();
			sp.setStaff(s);
			spd.save(sp);
			
			SalarySub ss=new SalarySub();
			ss.setStaff(s);
			ss.setSalaryAccout(sa);
			ss.setSubDate(new Date());
			ss.setSubStatic(0);
			SalaryPay pays = spd.findById(sp.getPayId()).get();
			ss.setSalaryPay(pays);
			ssd.save(ss);
	}	
	//送审
	public void xinzengss(int sub_id,Integer staffid,int staff_id,String type,String reason) {		
		SalarySub ss=ssd.findById(sub_id).get();
		Staff staffs = sd.findById(staffid).get();
		System.err.println(staffs+"-----"+sub_id+"--"+staffid+"--"+staff_id);
		ss.setShrId(staffs);
		ss.setSubDate(new Date());
		ss.setSubType(type);
		ss.setSubReason(reason);
		//申请人
		Staff staff1 = sd.findById(staff_id).get();
		ss.setStaff(staff1);
		ssd.save(ss);
	}
	//通过申请审批id查找审批人
	public JSONArray findsubshr(int sub_id) {
		return pm.findsubshr(sub_id);
	}
	//查询发放是否相同
	public JSONArray paylike() {
		return pm.pdthemandsa();
	}
	public JSONArray payaccount() {
		return pm.findaccounts();
	}
	public JSONArray account_idandmonth(int account_id) {
		return pm.account_idandmonth(account_id);
	}
	public JSONArray findaccountselect() {
		return pm.findaccountselect();
	}
}
